<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
set_html('title','登录大夫');
if($_RQ['op']=='get'){
	$_SESSION['userid']='';
	$_SESSION['role_id']='';
	$_SESSION['logout_forward']='jy_yiliao/worker.login/';
	$_SESSION['forward_url']='jy_yiliao/worker.home/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']=array();
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']));
	if(!$user || $post['password']!=base64_decode($user['password'])){
		exi('用户名或密码错误','error');
	}
	if(!$user['mid']){
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($user['password']));
		$uid=model('core/member')->register($member);
		pdo_update('jy_yiliao_worker',array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		//exi('该机构没有绑定用户/管理员！','error');
	}
	$_SESSION['userid']=$user['id'];
	$_SESSION['uid']=$user['mid']?:$uid;
	$_SESSION['role_id']=$post['role_id'];
	$_SESSION['member']['username']=$user['username'];
	//绝对地址，会刷新页面
	$url=custom_url('/worker.home/',true);
	exi('登陆成功!','',$url);
	
}